package com.mikepenz.app.activity.card;

import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.mikepenz.app.R;
import com.mikepenz.app.adapter.ListViewAdapter;
import com.mikepenz.presenter.CardListPresenter;
import com.mikepenz.presenter.imp.CardListPresenterImp;
import com.mikepenz.utils.CookicUntil;
import com.mikepenz.view.CardListView;
import com.quickdv.activity.BaseActivity;
import com.quickdv.until.ViewHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.InjectView;

/**
 * 我的卡片列表
 */
public class CardListActivity extends BaseActivity implements CardListView{

    List<Map<String, Object>> data;
    ListViewAdapter adapter;
    CardListPresenter presenter;
    @InjectView(android.R.id.list)
    ListView list;


    @Override
    public int getLayoutID() {
        return R.layout.activity_card_list;
    }

    public void initView() {
        adapter = new ListViewAdapter(this, data, R.layout.activity_card_list_item, new String[]{"cardCode"}, new int[]{R.id.card_code}){
            @Override
            protected void renderData(int position, View convertView) {
                CheckedTextView text = ViewHolder.get(convertView,R.id.card_code);


                if (data.get(position) !=null && ((String) data.get(position).get("cardCode")).equals(CookicUntil.getCardBean().getCardCode())){
                    text.setChecked(true);
                }else
                    text.setChecked(false);

                TextView tv =ViewHolder.get(convertView,R.id.card_money);
                tv.setText("余额:" + data.get(position).get("money") + "元");

                TextView binderTime = ViewHolder.get(convertView,R.id.card_bind_time);
                TextView number = ViewHolder.get(convertView,R.id.card_number);
                TextView status = ViewHolder.get(convertView,R.id.card_status);

                if (data.get(position).get("binderTime")!=null)
                    binderTime.setText("绑定时间:"+((String)data.get(position).get("binderTime")).substring(0,8));
                if ((boolean)data.get(position).get("binder") || data.get(position).get("money") !=null){
                    status.setText("可用");
                }

                number.setText("卡片"+ (position+1));

                super.renderData(position, convertView);
            }
        };
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                presenter.setSelect(i);
            }
        });
        presenter.getCardList();
    }

    @Override
    public void initActionbar() {

    }

    public void initData() {
        data = new ArrayList<>();
        presenter= new CardListPresenterImp(this);

    }

    @Override
    public void showError(String error) {
        Toast.makeText(this,error,Toast.LENGTH_LONG).show();
    }

    @Override
    public void notifyDataSetChanged() {
        adapter.notifyDataSetChanged();
    }

    @Override
    public void setDate(List<Map<String, Object>> date) {
        if (data != null) data.addAll(date);
    }
}
